const fs = require('fs');
const path = require('path');

module.exports = {
    // 查询
    query:function(whereKey, value){
        return new Promise(function(resolve,reject){
            fs.readFile(path.join(__dirname, '/temperature.json'), function(error, data) {
                if(error){
                    reject(error)
                }else{
                    resolve(JSON.parse(data).filter(item=>item[whereKey] === value))
                }
            })
        })
    },
    // 注册 
    reg: function(username, password){
        return new Promise(function(resolve,reject){
            fs.readFile(path.join(__dirname, './users.json'), 'utf-8', function(error, data) {
                if(error){
                    reject(error)
                }else{
                    let jsonData = data.toString();
                    jsonData = JSON.parse(jsonData);
                    jsonData.userId = jsonData.userId + 1
                    jsonData.data.push({
                        userId: jsonData.userId,
                        username: username,
                        password: password,
                        teamId:parseInt(Math.random()*10000) ,
                        regtime: String(Date.now()),
                    })
                    const str = JSON.stringify(jsonData, null, 5);
                    fs.writeFile(path.join(__dirname, './users.json'), str, function (err) {
                        if (err) {
                            resolve(false)
                        }else{
                            resolve(true)
                        }
                    })
                }
            })
        })    
    },
    // 登录
    login: function(username, password){
        return new Promise(function(resolve,reject){
            fs.readFile(path.join(__dirname, './users.json'), function(error, data) {
                if(error){
                    reject(error)
                }else{
                    resolve(JSON.parse(data).data.some(item=>item.username === username && item.password === password))
                }
            })
        })
    },
    // 检测
    checked: function(username){
        return new Promise(function(resolve,reject){
            fs.readFile(path.join(__dirname + '/users.json'), 'utf-8', function(error, data) {
                if(error){
                    reject(error)
                }else{
                    resolve(JSON.parse(data).data.some(item=>item.username === username))
                }
            })
        })
    },
    // 增加
    create:function(whereKey, value){
        return new Promise(function(resolve,reject){
            fs.readFile(path.join(__dirname, '/temperature.json'), function(error, data) {
                if(error){
                    reject(error)
                }else{
                    let jsonData = data.toString();
                    jsonData = JSON.parse(jsonData);
                    jsonData.id = jsonData.id + 1
                    jsonData.data.push({
                        // id: jsonData.id,
                        // username: username,
                        // password: password,
                        // reg_time: String(Date.now())
                    })
                    const str = JSON.stringify(jsonData, null, 4);
                    fs.writeFile(path.join(__dirname, './users.json'), str, function (err) {
                        if (err) {
                            resolve(false)
                        }else{
                            resolve(true)
                        }
                    })
                }
            })
        })
    },
    // 修改
    update:function(whereKey, value){
        return new Promise(function(resolve,reject){
            
        })
    },
    // 删除
    delete:function(whereKey, value){
        return new Promise(function(resolve,reject){
           
        })
    },
}